package com.example.demo1122.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo1122.domain.LoginTeacher;
import com.example.demo1122.entity.Teacher;
import com.example.demo1122.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class TeacherUserDetailsService implements UserDetailsService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public UserDetails loadUserByUsername(String teachername) throws UsernameNotFoundException {
        //查询用户信息
        LambdaQueryWrapper<Teacher> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(Teacher::getTeacherID,teachername);
        Teacher teacher=teacherMapper.selectOne(queryWrapper);
        //判断是否为空
        if(Objects.isNull(teacher)){
            throw new RuntimeException("用户名或密码错误");
        }
        //TODO 根据用户查询权限信息 添加到LoginUser中

        //封装成UserDetails对象返回
        return new LoginTeacher(teacher);

    }
}
